package com.exde.wcsiapp.gps;

import android.content.Context;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;

public class AMapLocationUtil implements AMapLocationListener {

    private AMapLocationClient locationClient = null;
    private AMapLocationClientOption locationOption = null;
    private Context context;

    private AMapLocation location = null;
    private Gps gps = null;

    public AMapLocationUtil(Context context) {
        this.context = context;
        initParams();
    }

    public AMapLocation getAMapLocation(){
        return location;
    }
    public Gps getGps(){
        return gps;
    }


    public void startLocation() {
        Log.d("MainActivity", "startLocation: "+locationClient.isStarted());
        //启动定位
        locationClient.startLocation();

    }

    public void stopLocation() {
        location = null;
        gps = null;
        if (locationClient != null && locationClient.isStarted()) {
            locationClient.stopLocation();
        }
    }

    /*获取定位数据*/
    public void initParams() {
        //初始化定位
        locationClient = new AMapLocationClient(context);
        //设置定位回调监听
        locationClient.setLocationListener(this);
        //初始化定位参数
        locationOption = new AMapLocationClientOption();
        //设置定位模式为Hight_Accuracy高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
        locationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置是否返回地址信息（默认返回地址信息）
        locationOption.setNeedAddress(true);
        //设置是否只定位一次,默认为false
        locationOption.setOnceLocation(false);
        //设置是否强制刷新WIFI，默认为强制刷新
        locationOption.setWifiActiveScan(false);
        //设置是否允许模拟位置,默认为false，不允许模拟位置
        locationOption.setMockEnable(false);
        //设置定位间隔,单位毫秒,默认为2000ms
        locationOption.setInterval(2000);
        // 关闭缓存机制
        locationOption.setLocationCacheEnable(false);
        //给定位客户端对象设置定位参数
        locationClient.setLocationOption(locationOption);
    }

    // 定位监听
    @Override
    public void onLocationChanged(AMapLocation loc) {
        Log.d("MainActivity", "AMap OnLocationChanged");
        if (loc != null){
            Log.d("MainActivity", "AMap getErrorCode=" + loc.getErrorCode());
            Log.d("MainActivity", "AMap getLocationType=" + loc.getLocationType());
            if (loc.getErrorCode() == 0) {
                location = loc;
                Log.d("MainActivity", "AMap lat=" + location.getLatitude());
                Log.d("MainActivity", "AMap lon=" + location.getLongitude());
                Log.d("MainActivity", "AMap acc=" + location.getAccuracy());//获取精度信息());

                gps = PositionUtil.gcj_To_Gps84(loc.getLatitude(),loc.getLongitude());
                Log.d("MainActivity", "Gps84 lat=" + gps.getWgLat());
                Log.d("MainActivity", "Gps84 lon=" + gps.getWgLon());
//                if(location.getAccuracy() <= 50) {
                    SendHandlerMessageUtil.setGps(gps);
                    SendHandlerMessageUtil.setLocation();
                    stopLocation();
//                }
            }
        }
    }
}
